package service.dropdown.impl;

import java.util.List;

import model.dropdown.DCommon;
import model.dropdown.DUnitcat;

import dao.dropdown.DCommonDao;
import dao.dropdown.DIdtypeDao;
import dao.dropdown.DIndustrycatADao;
import dao.dropdown.DIndustrycatBDao;
import dao.dropdown.DUnitcatDao;
import exception.Exceptions;
import service.dropdown.DropDownService;

public class DropDownServiceImpl implements DropDownService{
	private DIndustrycatADao dIndustrycatADao;
	private DIndustrycatBDao dIndustrycatBDao;
	private DUnitcatDao dUnitcatDao;
	private DIdtypeDao dIdtypeDao;
	private DCommonDao dCommonDao;


	

	public DCommonDao getdCommonDao() {
		return dCommonDao;
	}

	public void setdCommonDao(DCommonDao dCommonDao) {
		this.dCommonDao = dCommonDao;
	}

	public DIdtypeDao getdIdtypeDao() {
		return dIdtypeDao;
	}

	public void setdIdtypeDao(DIdtypeDao dIdtypeDao) {
		this.dIdtypeDao = dIdtypeDao;
	}

	public DUnitcatDao getdUnitcatDao() {
		return dUnitcatDao;
	}

	public void setdUnitcatDao(DUnitcatDao dUnitcatDao) {
		this.dUnitcatDao = dUnitcatDao;
	}

	public DIndustrycatBDao getdIndustrycatBDao() {
		return dIndustrycatBDao;
	}

	public void setdIndustrycatBDao(DIndustrycatBDao dIndustrycatBDao) {
		this.dIndustrycatBDao = dIndustrycatBDao;
	}

	public DIndustrycatADao getdIndustrycatADao() {
		return dIndustrycatADao;
	}

	public void setdIndustrycatADao(DIndustrycatADao dIndustrycatADao) {
		this.dIndustrycatADao = dIndustrycatADao;
	}

	@Override
	public List<?> getDIndustrycatAList() {
		return dIndustrycatADao.findAll();
	}
	

	@Override
	public List<?> getDIndustrycatBList(int pid) {
		return dIndustrycatBDao.findList("pid",pid);
	}




	@Override
	public List<?> getDUnitcatList() {
		
		return dUnitcatDao.findAll();
	}

	@Override
	public void updateUc(DUnitcat uc) {
		// TODO Auto-generated method stub
		dUnitcatDao.saveOrUpdate(uc);
	}

	@Override
	public void deleteUc(DUnitcat uc) {
		// TODO Auto-generated method stub
		dUnitcatDao.delete(uc);
	}

	/*@Override
	public int exist(DUnitcat uc) {
		// TODO Auto-generated method stub
		if(dUnitcatDao.findOne("id", uc.getId()) != null)
			return 1;
		else if(dUnitcatDao.findOne("name", uc.getName()) != null)
			return 2;
		return 0;
	}
	*/

	@Override
	public List<?> getDIdtypeList() {
		// TODO Auto-generated method stub
		return dIdtypeDao.findAll();
	}

	@Override
	public List<DCommon> getCommonList(int type, int pcode) {
		// TODO Auto-generated method stub
		String hql = "type="+type+" and pcode="+pcode;
		return dCommonDao.findModel(hql);
	}

	@Override
	public void updateCommon(DCommon cm) {
		// TODO Auto-generated method stub
		dCommonDao.saveOrUpdate(cm);
	}
	
	public void deleteCommon(DCommon cn){
		dCommonDao.delete(cn);
	}

	@Override
	public int exist(DCommon cm) {
		// TODO Auto-generated method stub
		String hql = "type="+cm.getType()+" and code="+cm.getCode();
		

		if(dCommonDao.findModel(hql) != null){
			return 1;
		}
		hql = "type="+cm.getType()+" and name='"+cm.getName()+"'";
		System.out.println(hql);
		if(dCommonDao.findModel(hql) != null){
			return 2;
		}
		System.out.println("inhere");
		return 0;
	}

	@Override
	public List<?> getListBean(int type,int pcode) throws Exceptions {
		String select = "new vo.CommonBean(d.code, d.name)";
		String from = "DCommon as d where d.type = " + type + " and d.pcode = " + pcode;
		return this.dCommonDao.findQuery(select, from);
	}

	@Override
	public DCommon findById(int id) {
		// TODO Auto-generated method stub
		return dCommonDao.findOne("id", id);
	}
	
	
}
